// mircoApp/freeCharge/freeCharge.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    timeData: {},
    endTime:'',
    presentTime:"",
    localStatus:false,
    nowFree:'',
    freeFlag:1,
    freeId:'',
    lotteryList:[],
    timeSet:null,
    imgPath: app.globalData.imgPath,
    freeImg:`${app.globalData.imgPath}free-order-return/free-bg.png`
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.data.nowFree = new Date().getTime();
    if(options.id) {
      this.setData({
        freeId:options.id
      })
      this.freeDetail(options.id);
    } else {
      this.getFreeId()
    }
  },
  onChange(e) {
    this.setData({
      timeData: e.detail,
    });
  },
  goBack() {
    wx.navigateTo({
      url: '/mircoApp/shop_base/reward_template/index?api=plugin.free-order-return.api.index.index',
    })
     if (this.data.timeSet) {
      clearInterval(this.data.timeSet);
    }
  },
  freeDetail(id) {
    let urlStr = app.getNetAddresss("plugin.free-order-return.api.index.record-detail");
    app._postNetWork({
      url: urlStr,
      data: {basic_info:1,id:id},
      success: (resdata) => {
        var res = resdata.data;
        let data = res.data;
        let date = new Date(data.end_time);
        let participant = data.participant;
        if(participant.length == 1) {
          participant.push(data.participant[0])
        }
        if(data.status) {
          clearInterval(this.data.timeSet);
        }
        if(this.data.nowFree >= date && !data.status) {
          this.data.localStatus = true;
          if (this.data.timeSet == null) {
            this.data.timeSet = setInterval(() => {
              this.freeDetail(this.data.freeId);
            }, 3000);
          }
        } else {
          this.data.presentTime = date - this.data.nowFree;
          this.data.localStatus = false;
        }
        
        this.setData({
          winningArr:data.choosen,
          lotteryList:participant,
          status:data.status,
          endTime:date,
          presentTime:this.data.presentTime,
          localStatus:this.data.localStatus
        })
      },
      fail: function (res) {
        console.log(res);
      },
    });
  },
  getFreeId() {
    let urlStr = app.getNetAddresss("plugin.free-order-return.api.index.get-latest-id");
    app._postNetWork({
      url: urlStr,
      data: {},
      success: (resdata) => {
        var res = resdata.data;
        let data = res.data;
        this.data.freeId = data.id;
        this.freeDetail(data.id);
      },
      fail: function (res) {
        console.log(res);
      },
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {
    if (this.data.timeSet) {
      clearInterval(this.data.timeSet);
    }
    
    // this.lotteryList = [];
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {
    if (this.data.timeSet) {
      clearInterval(this.data.timeSet);
    }
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})